package com.plz.afmt.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.plz.afmt.entity.UserEntity;
import com.plz.afmt.service.UserService;
import com.plz.common.exception.BusinessException;
import com.plz.web.util.PageCommonUtil;
import com.plz.web.base.vo.PagerResult;
import com.plz.afmt.dto.EnterprisesDto;
import com.plz.afmt.dto.EnterprisesQueryDto;
import com.plz.afmt.entity.EnterprisesEntity;
import com.plz.afmt.mapper.EnterprisesMapper;
import com.plz.afmt.service.EnterprisesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plz.afmt.transfer.EnterprisesTransfer;
import com.plz.afmt.vo.EnterprisesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 企业表
 *
 * @author jzc
 */
@Service
public class EnterprisesServiceImpl extends ServiceImpl<EnterprisesMapper, EnterprisesEntity> implements EnterprisesService {
    @Autowired
    private EnterprisesTransfer enterprisesTransfer;
    @Autowired
    private UserService userService;

    @Override
    public PagerResult<EnterprisesVo> pageQuery(EnterprisesQueryDto dto) {
        QueryWrapper<EnterprisesEntity> queryWrapper = PageCommonUtil.dtoToWrapper(EnterprisesEntity.class, dto);
        queryWrapper.orderByDesc("id");
        PagerResult<EnterprisesEntity> entityPagerResult = PageCommonUtil.sigelPage(dto, queryWrapper, this::page);
        PagerResult<EnterprisesVo> voPagerResult = enterprisesTransfer.pagePo2PageVo(entityPagerResult);
        return voPagerResult;
    }

    @Override
    public EnterprisesEntity add(EnterprisesDto dto) {
        EnterprisesEntity entity = new EnterprisesEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setCode(getCode());
        this.saveOrUpdate(entity);
        return entity;
    }

    private String getCode() {
        String code = UUID.randomUUID().toString().substring(0, 8);
        return code;
    }

    @Override
    public EnterprisesEntity update(EnterprisesDto dto) {
        EnterprisesEntity entity = new EnterprisesEntity();
        BeanUtils.copyProperties(dto, entity);
        this.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public List<EnterprisesEntity> addOrUpdateList(List<EnterprisesDto> dto) {
        List<EnterprisesEntity> entitys = enterprisesTransfer.listDto2listPo(dto);
        this.saveOrUpdateBatch(entitys);
        return entitys;
    }

    @Override
    public List<EnterprisesVo> listFor() {
        List<EnterprisesEntity> list = this.list();
        List<EnterprisesVo> vos = enterprisesTransfer.listPo2listVo(list);
        return vos;
    }

    @Override
    public EnterprisesVo detail(Long id) {
        EnterprisesEntity entity = this.getById(id);
        EnterprisesVo vo = new EnterprisesVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateVipTime(EnterprisesDto dto) {
        if (dto.getVipStartTime().isAfter(dto.getVipEndTime())) {
            throw new BusinessException("开始时间不能在结束时间之后！");
        }
        boolean update = this.update(new LambdaUpdateWrapper<EnterprisesEntity>()
                .eq(EnterprisesEntity::getId, dto.getId())
                .set(EnterprisesEntity::getVipStartTime, dto.getVipStartTime())
                .set(EnterprisesEntity::getVipEndTime, dto.getVipEndTime()));
        if (update) {
            List<UserEntity> userEntities = userService.listByEnterprisesCode(dto.getCode());
            userEntities.forEach(userEntity -> {
                userEntity.setVipStatus(1);
                userEntity.setLiberalTime(dto.getVipStartTime());
                userEntity.setExpiredTime(dto.getVipEndTime());
            });
            userService.saveOrUpdateBatch(userEntities);
        }
        return true;
    }

    @Override
    public EnterprisesEntity getByCode(String enterprisesCode) {
        //todo 需优化从缓存中取
        return this.getOne(new LambdaQueryWrapper<EnterprisesEntity>()
                .eq(EnterprisesEntity::getCode, enterprisesCode));
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void task() {
        List<EnterprisesEntity> list = this.list(new LambdaQueryWrapper<EnterprisesEntity>()
                .ge(EnterprisesEntity::getVipEndTime, LocalDateTime.now()));
        for (EnterprisesEntity enterprisesEntity : list) {
            List<UserEntity> userEntities = userService.listByEnterprisesCode(enterprisesEntity.getCode());
            userEntities.forEach(userEntity -> {
                userEntity.setVipStatus(2);
            });
            userService.saveOrUpdateBatch(userEntities);
        }
    }

}



